package com.qidian.hw.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qidian.hw.pojo.CaseInfo;
import com.qidian.hw.pojo.Product;
import com.qidian.hw.pojo.ServiceInfo;
import com.qidian.hw.service.ServiceInfoService;
import com.qidian.hw.service.impl.ServiceInfoServiceImpl;
import com.qidian.hw.tools.Const;
import com.qidian.hw.tools.JSONTools;
import com.qidian.hw.tools.dto.Pager;

/**
* @author 郭浩
*
*@company 起点编程
*
* 2022年8月19日 下午7:47:18
*/
public class ServiceInfoServlet extends HttpServlet{
	ServiceInfoService serviceInfoService = new ServiceInfoServiceImpl();
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String op = req.getParameter("op");
		switch (op) {
			//保存
		case "save":
			save(req,resp);
			break;
			//回显
		case "input":
			input(req,resp);
			break;
			//表单传回值修改
		case "update":
			update(req,resp);
			break;
			//列表
		case "list":
			list(req,resp);
			break;
			//删除
		case "delete":
			delete(req,resp);
			break;
		case "deletes":
			deletes(req,resp);
			break;
		case "query":
			query(req,resp);
			break;
		default:
			super.service(req, resp);
		}
	}
	
	public void deletes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String result = "0";
		String [] ids = req.getParameterValues("serviceId");
		if(ids!=null && ids.length > 0) {
			result = serviceInfoService.deletes(ids) + "";
		}
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result);
	}
	
	public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String serviceTitle = req.getParameter("serviceTitle");
		String servicetDesc = req.getParameter("servicetDesc");
		String serviceImg = req.getParameter("serviceImg");
		ServiceInfo serviceInfo = new ServiceInfo(serviceTitle, servicetDesc, serviceImg);
		int result = serviceInfoService.serviceInfoSave(serviceInfo);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String serviceIdStr = req.getParameter("serviceId");
		long serviceId = 0;
		try {
			serviceId = Long.parseLong(serviceIdStr);
		} catch (Exception e) {
		}
		ServiceInfo serviceInfo = serviceInfoService.queryServiceInfoById(serviceId);
		String serviceInfoStr = JSONTools.object2json(serviceInfo);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(serviceInfoStr);
	}
	
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String serviceIdStr = req.getParameter("serviceId");
		long serviceId = 0;
		try {
			serviceId = Long.parseLong(serviceIdStr);
		}catch(Exception e) {}
		String serviceTitle = req.getParameter("serviceTitle");
		String serviceDetails = req.getParameter("serviceDetails");
		String serviceImg = req.getParameter("serviceImg");
		ServiceInfo serviceInfo = new ServiceInfo(serviceTitle, serviceDetails, serviceImg);
		serviceInfo.setServiceId(serviceId);
		int result = serviceInfoService.serviceInfoUpdate(serviceInfo);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<ServiceInfo> al = new ArrayList<ServiceInfo>();
		al = serviceInfoService.serviceInfoList();
		String serviceInfosStr = JSONTools.object2json(al);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(serviceInfosStr);
	}
	
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String serviceInfoIdStr = req.getParameter("serviceInfoId");
		long serviceInfoId = 0;
		try {
			serviceInfoId = Long.parseLong(serviceInfoIdStr);
		} catch (Exception e) {
		}
		int result = serviceInfoService.serviceInfoDelete(serviceInfoId);
		resp.setContentType(Const.RES_PLAIN);
		resp.getWriter().write(result + "");
	}
	
	public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String title = req.getParameter("title");
		String pageStr = req.getParameter("page");
		String sizeStr = req.getParameter("size");
		int page = 1;
		int size = 10;
		try {
			page = Integer.parseInt(pageStr);
			size = Integer.parseInt(sizeStr);
		} catch (Exception e) {
		}
		Pager pager = serviceInfoService.queryByPage(page, size, title);
		String pagerStr = JSONTools.object2json(pager);
		resp.setContentType(Const.RES_JSON);
		resp.getWriter().write(pagerStr);
	}
}
